﻿var SplitPage = (function () {
    return {
        Static: { Main: null, PageSize: 10, CurrentPage: 1, Total: 0, colspan: 0, PageCount: 0, first: false, colspan: 0, Control: "", Condition: "", Rs: [], json: {}, Station: false, PageName: "", Action: "" }
        , doc: document, databody: "", footer: "", PowerID: "", PerArr: [], QXArr: [], PesronList: {}
        , StyleName: "grcspxp"
        , tdLastHtml: {
            "PQ0001": ""
          , "PQ0002": "<a href='javascript:;' action='delete'><img src='/View/Image/010.gif' border='0' width='10' height='8'>[删除]</a>"
          , "PQ0003": "<a href='javascript:;' action='edit'><img src='/View/Image/037.gif' border='0' width='10' height='8'>[修改]</a>"
          , "PQ0004": ""
          , "PQ0005": "<a href='javascript:;' action='kaiban'><img src='/View/Image/right_jiantou.gif'>[开班]</a>"
          , "PQ0006": "<a href='javascript:;' action='copy'><img src='/View/Image/plus.gif'>[复制]</a>"
          , "PQ0007": "<a href='javascript:;' action='copy'>[销售汇总]</a>"
          , "PQ0008": "<a href='javascript:;' action='copy'>[预订]</a>"
        }
        , _$: function (id) { return document.getElementById(id); }
               , InitPage: function (obj) {
                   var that = this;
                   this.GetData(obj);
               }
               , InitEvent: function (obj) {
                   $("#" + this.databody).mouseover(function (e) {
                       var e = e || window.event;
                       var target = e.srcElement || e.target;
                       if ($(target.parentNode).is("tr")) {
                           $(target.parentNode).addClass("trOver").removeClass("trOut");
                           (! -[1, ]) ? e.cancelBubble = true : e.stopPropagation();
                       }
                   }).mouseout(function (e) {
                       var e = e || window.event;
                       var target = e.srcElement || e.target;
                       if ($(target.parentNode).is("tr")) {
                           $(target.parentNode).addClass("trOut").removeClass("trOver");
                           (! -[1, ]) ? e.cancelBubble = true : e.stopPropagation();
                       }
                   });
               }
               , GetData: function (obj) {
                   var jsonArr = null;
                   var that = this;
                   that.Static.PageName = obj.PageName; //页面名称
                   that.Static.colspan = obj.colspan;
                   that.Static.Control = obj.Control;
                   that.Static.Condition = obj.Condition;
                   if (typeof (obj.CurrentPage) != "undefined") {
                       that.Static.CurrentPage = obj.CurrentPage;
                   }
                   that.Static.Action = obj.Action;
                   this.databody = obj.databody;
                   this.footer = obj.footer;
                   this.Static.Station = obj.Station;
                   this.InitEvent();
                   that.Static.json = { PageSize: that.Static.PageSize, CurrentPage: that.Static.CurrentPage, Action: that.Static.Action, Condition: obj.Condition };
                   if (obj.BtnID) {
                       that.Static.json["BtnID"] = obj.BtnID;
                   }
                   $.post(obj.Control, that.Static.json, function (data) {
                       if (data !== "") {
                           var Arr = data.split("@");
                           if (Arr[0] !== "") {
                               jsonArr = eval("(" + Arr[0] + ")");
                               that.Assembly(jsonArr); //拼装得到数据的主体部分
                           }
                           else {
                               jsonArr = "";
                               $("#" + SplitPage.databody).html("<tr><td colspan=" + that.Static.colspan + " class='grcspxp'>表中无数据</td></tr>");
                           }
                           that.Static.Total = parseInt(Arr[1]);
                           //that.GetPermission(jsonArr);
                           if (!obj.first) {
                               $("#currentspan").html("当前第" + that.Static.CurrentPage + "页");
                               return false;
                           }
                           that.GetFooterHtml(); //拼装得到页尾部分
                       }
                       else {
                           $("#" + SplitPage.databody).html("<tr><td colspan=" + that.Static.colspan + " class='grcspxp'>表中无数据</td></tr>");
                       }
                   });
               }
               , Search: function (obj) {
                   this.Static.Condition = obj.Condition;
                   this.Static.CurrentPage = 1; //搜索从第一页开始
                   this.GetData({ "first": "Search", "Control": this.Static.Control
                   , "colspan": this.Static.colspan, "Action": that.Static.Action
                   , PageSize: this.Static.PageSize, CurrentPage: this.Static.CurrentPage
                   , Condition: this.Static.Condition, "first": true
                   });
               }
               , Go: function (obj) { //跳到指定的页面
                   switch (obj.id) {
                       case "first":
                           this.Static.CurrentPage = 1;
                           break;
                       case "prev":
                           if (this.Static.CurrentPage == 1) {
                               return false;
                           }
                           else {
                               this.Static.CurrentPage--;
                           }
                           break;
                       case "next":
                           if (this.Static.CurrentPage == this.Static.PageCount) {
                               return false;
                           }
                           else {
                               this.Static.CurrentPage++;
                           }
                           break;
                       case "last":
                           this.Static.CurrentPage = this.Static.PageCount;
                           break;
                       case "SelectPageIndex":
                           this.Static.CurrentPage = parseInt($(obj).val());
                           break;
                   }
                   $("#SelectPageIndex")[0].selectedIndex = this.Static.CurrentPage - 1;
                   this.GetData({ Control: this.Static.Control, colspan: this.Static.colspan, "Action": this.Static.Action, PageSize: this.Static.PageSize, CurrentPage: this.Static.CurrentPage, Condition: this.Static.Condition, PageName: this.Static.PageName, databody: this.databody, footer: this.footer });
               }
               , GetFooterHtml: function (obj) { //拼装页脚部分
                   if (this.Static.Total <= this.Static.PageSize) {
                       this.Static.PageCount = 1;
                   }
                   else {
                       this.Static.PageCount = (this.Static.Total % this.Static.PageSize == 0 ? (this.Static.Total / this.Static.PageSize) : Math.floor(this.Static.Total / this.Static.PageSize) + 1);
                   }
                   var FooterHtml = "";
                   FooterHtml = "<tr><td colspan='" + this.Static.colspan + "'>";
                   FooterHtml += "<div>";
                   FooterHtml += "<ul><li class='left_footer'><span style='margin-left: 0px;'>总共" + this.Static.PageCount + "页</span><span  id='currentspan'>当前第" + this.Static.CurrentPage + "页</span>";
                   FooterHtml += "<span id='first' onclick='SplitPage.Go(this);'>首页</span><span id='prev' onclick='SplitPage.Go(this);'>上一页</span> <span id='next' onclick='SplitPage.Go(this);'>下一页</span> <span id='last' onclick='SplitPage.Go(this);'>尾页</span></li>";
                   FooterHtml += "<li class='right_footer' style='float: right;'><span>跳转到第</span><span>";
                   FooterHtml += "<select id='SelectPageIndex' onchange='SplitPage.Go(this);' style='width:60px; height:20px;'>";
                   for (var i = 0; i < this.Static.PageCount; i++) {
                       FooterHtml += "<option value=" + (i + 1) + ">" + (i + 1) + "/" + this.Static.PageCount + "</option>";
                   }
                   FooterHtml += "</select>";
                   FooterHtml += "</span><span>页</span> </li>";
                   FooterHtml += "</ul>";
                   FooterHtml += "</div>";
                   FooterHtml += "</td></tr>";
                   $("#" + this.footer).html(FooterHtml);
               }
               , ControlStyle: function () {
                   if (!document.title.localeCompare("产品管理")) {
                       this.StyleName = "grcspxptd";
                   }
               }
               , Assembly: function (jsonArr) { //拼装数据主体部分
                   var that = this;
                   var Bodyhtml = "", data = "";
                   that.Static.PesronList = {};
                   for (var i = 0; i < jsonArr.length; i++) {
                       Bodyhtml += "<tr main=" + jsonArr[i]["main"] + ">";
                       for (var j in jsonArr[i]) {
                           if (j !== "main") {
                               if (jsonArr[i][j].indexOf("0:00:00") > 0) {
                                   data = jsonArr[i][j].split(" ")[0];
                               }
                               else {
                                   data = jsonArr[i][j];
                               }
                               Bodyhtml += "<td class='grcs'>" + data + "</td>";
                           }
                       }
                       this.ControlStyle();
                       if (that.Static.PageName == "NoOperation") {
                           // 无任何操作
                       }
                       else {
                           Bodyhtml += "<td class='" + this.StyleName + "'><a href='javascript:;' action='choose'>[选择]</a></td>";
                       }
                       Bodyhtml += "</tr>";
                   }
                   $("#" + that.databody).html(Bodyhtml);
                   if (that.Static.PageName == "OrderDetail") {
                       $("#" + that.databody + " tr").each(function () {
                           $(this).find("td:last").remove();
                       })
                   }
               }
    }
} ());